<template>
  <ContentWrap>
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="page-title">
          <Icon icon="ep:microphone" class="title-icon" />
          <div class="title-text">
            <h2>语音管理</h2>
            <p>管理系统中的语音文件和TTS合成</p>
          </div>
        </div>
      </div>
    </div>

    <!-- 搜索工作栏 -->
    <div class="search-container">
      <el-form
        class="search-form"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="80px"
      >
        <div class="search-row">
          <el-form-item label="文件名称" prop="name" class="search-item">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入文件名称"
              clearable
              @keyup.enter="handleQuery"
              class="search-input"
            >
              <template #prefix>
                <Icon icon="ep:document" class="input-icon" />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="上传类型" prop="type" class="search-item">
            <el-select
              v-model="queryParams.type"
              placeholder="请选择上传类型"
              clearable
              class="search-select"
            >
              <el-option label="手动" :value="1">
                <div class="option-item">
                  <Icon icon="ep:upload" class="option-icon" />
                  <span>手动上传</span>
                </div>
              </el-option>
              <el-option label="语音合成" :value="2">
                <div class="option-item">
                  <Icon icon="ep:cpu" class="option-icon" />
                  <span>语音合成</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="TTS厂商" prop="tts" class="search-item">
            <el-select
              v-model="queryParams.tts"
              placeholder="请选择TTS厂商"
              clearable
              class="search-select"
            >
              <el-option label="科大讯飞" :value="2">
                <div class="option-item">
                  <Icon icon="ep:microphone" class="option-icon" />
                  <span>科大讯飞</span>
                </div>
              </el-option>
            </el-select>
          </el-form-item>
        </div>

        <div class="search-actions">
          <el-button class="search-btn" @click="handleQuery">
            <Icon icon="ep:search" class="btn-icon" />
            <span class="btn-text">搜索</span>
          </el-button>
          <el-button class="reset-btn" @click="resetQuery">
            <Icon icon="ep:refresh" class="btn-icon" />
            <span class="btn-text">重置</span>
          </el-button>
          <el-button
            type="primary"
            class="add-btn"
            @click="openForm('create')"
            v-hasPermi="['call:task:create']"
          >
            <Icon icon="ep:plus" class="btn-icon" />
            <span class="btn-text">新增语音</span>
          </el-button>
        </div>
      </el-form>
    </div>

    <!-- 列表容器 -->
    <div class="table-container">
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        class="voice-table"
      >
        <el-table-column label="文件名称" align="center" prop="name" min-width="150">
          <template #default="scope">
            <div class="name-cell">
              <Icon icon="ep:document" class="file-icon" />
              <span class="file-name">{{ scope.row.name }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="类型" align="center" prop="type" width="120">
          <template #default="scope">
            <el-tag :type="scope.row.type == 1 ? 'primary' : 'success'" class="type-tag">
              <Icon :icon="scope.row.type == 1 ? 'ep:upload' : 'ep:cpu'" class="tag-icon" />
              {{ scope.row.type == 1 ? '手动' : '语音合成' }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="TTS方式" align="center" prop="tts" width="120">
          <template #default="scope">
            <el-tag type="primary" class="tts-tag" v-if="scope.row.tts">
              <Icon icon="ep:microphone" class="tag-icon" />
              {{ ttsMap[scope.row.tts] }}
            </el-tag>
            <span v-else class="no-data">-</span>
          </template>
        </el-table-column>

        <el-table-column label="合成文本" align="center" prop="content" min-width="200">
          <template #default="scope">
            <div class="content-cell" v-if="scope.row.content">
              <Icon icon="ep:chat-line-square" class="content-icon" />
              <span class="content-text">{{ scope.row.content }}</span>
            </div>
            <span v-else class="no-data">-</span>
          </template>
        </el-table-column>

        <el-table-column label="文件地址" align="center" prop="filePath" min-width="250">
          <template #default="scope">
            <div class="file-path-cell" @click="copy(scope.row.filePath)">
              <Icon icon="ep:link" class="path-icon" />
              <span class="path-text">{{ scope.row.filePath }}</span>
              <Icon icon="ep:copy-document" class="copy-icon" />
            </div>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="220" fixed="right">
          <template #default="scope">
            <div class="action-buttons">
              <el-button
                link
                type="primary"
                class="action-btn edit-btn"
                @click="openForm('update', scope.row.id)"
              >
                <Icon icon="ep:edit" class="action-icon" />
                <span class="action-text">编辑</span>
              </el-button>
              <el-button
                link
                type="danger"
                class="action-btn delete-btn"
                @click="handleDelete(scope.row.id)"
              >
                <Icon icon="ep:delete" class="action-icon" />
                <span class="action-text">删除</span>
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
          class="custom-pagination"
        />
      </div>
    </div>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <VoiceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { Edit } from '@element-plus/icons-vue'
import download from '@/utils/download'
import * as VoiceApi from '@/api/call/voiceManagement'
import { dateFormatter } from '@/utils/formatTime'
import VoiceForm from './components/voiceForm.vue'

defineOptions({ name: 'voiceManagement' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: null, //文件名称
  type: null, //上传类型  1手动 2语音合成
  tts: null //tts厂商  1腾讯
})
const ttsMap = {
  1: '腾讯',
  2: '科大讯飞'
}
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await VoiceApi.getIvrVoicePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await VoiceApi.deleteIvrVoice(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const copy = (val) => {
  navigator.clipboard
    .writeText(val)
    .then(() => {
      ElMessage.success('复制成功')
    })
    .catch((err) => {
      ElMessage.error('无法复制文本: ', err)
    })
}
const { push } = useRouter()
/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
// 页面头部
.page-header {
  margin-bottom: 24px;
  padding: 10px 24px;
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border-radius: 16px;
  color: white;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.page-title {
  display: flex;
  align-items: center;
  gap: 16px;
}

.title-icon {
  font-size: 32px;
  color: rgba(255, 255, 255, 0.9);
}

.title-text {
  h2 {
    font-size: 24px;
    font-weight: 600;
    margin: 0 0 4px 0;
    color: white;
  }

  p {
    font-size: 14px;
    margin: 0;
    color: rgba(255, 255, 255, 0.8);
  }
}

// 搜索容器
.search-container {
  background: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.search-form {
  .search-row {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    margin-bottom: 20px;
  }

  .search-item {
    flex: 1;
    min-width: 200px;
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #374151;
    }
  }
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focus {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }

  .input-icon {
    color: #9ca3af;
    font-size: 16px;
  }
}

.search-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: 8px;
    border: 2px solid #e5e7eb;
    transition: all 0.3s ease;

    &:hover {
      border-color: #d1d5db;
    }

    &.is-focused {
      border-color: #667eea;
      box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
    }
  }
}

.option-item {
  display: flex;
  align-items: center;
  gap: 8px;

  .option-icon {
    font-size: 14px;
    color: #6b7280;
  }
}

.search-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.search-btn,
.reset-btn,
.add-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 20px;
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;

  .btn-icon {
    font-size: 16px;
  }

  &:hover {
    transform: translateY(-1px);
  }
}

.search-btn {
  background: #f3f4f6;
  border: 2px solid #e5e7eb;
  color: #374151;

  &:hover {
    background: #e5e7eb;
    border-color: #d1d5db;
  }
}

.reset-btn {
  background: #fef3c7;
  border: 2px solid #fbbf24;
  color: #92400e;

  &:hover {
    background: #fde68a;
    border-color: #f59e0b;
  }
}

.add-btn {
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border: none;
  color: white;

  &:hover {
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
  }
}

// 表格容器
.table-container {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border: 1px solid #e5e7eb;
}

.voice-table {
  :deep(.el-table__header) {
    background: #f8fafc;

    th {
      background: #f8fafc !important;
      color: #374151;
      font-weight: 600;
      border-bottom: 2px solid #e5e7eb;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.2s ease;

      &:hover {
        background: #f8fafc !important;
      }

      td {
        padding: 16px 12px;
        border-bottom: 1px solid #f1f5f9;
      }
    }
  }
}

// 表格单元格样式
.name-cell {
  display: flex;
  align-items: center;
  gap: 8px;

  .file-icon {
    color: #667eea;
    font-size: 16px;
  }

  .file-name {
    font-weight: 500;
    color: #374151;
  }
}

.type-tag,
.tts-tag {
  display: flex;
  align-items: center;
  gap: 4px;
  border-radius: 6px;
  font-weight: 500;

  .tag-icon {
    font-size: 12px;
  }
}

.content-cell {
  display: flex;
  align-items: center;
  gap: 8px;

  .content-icon {
    color: #10b981;
    font-size: 16px;
    flex-shrink: 0;
  }

  .content-text {
    color: #374151;
    line-height: 1.4;
  }
}

.file-path-cell {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.2s ease;

  &:hover {
    background: #f0f9ff;

    .copy-icon {
      opacity: 1;
    }
  }

  .path-icon {
    color: #0ea5e9;
    font-size: 16px;
    flex-shrink: 0;
  }

  .path-text {
    color: #0369a1;
    font-size: 13px;
    word-break: break-all;
    flex: 1;
  }

  .copy-icon {
    color: #0ea5e9;
    font-size: 14px;
    opacity: 0;
    transition: opacity 0.2s ease;
  }
}

.no-data {
  color: #9ca3af;
  font-style: italic;
}

// 操作按钮
.action-buttons {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s ease;

  .action-icon {
    font-size: 14px;
  }

  &.edit-btn {
    &:hover {
      background: rgba(59, 130, 246, 0.1);
      color: #3b82f6;
    }
  }

  &.delete-btn {
    &:hover {
      background: rgba(239, 68, 68, 0.1);
      color: #ef4444;
    }
  }
}

// 分页容器
.pagination-container {
  display: flex;
  justify-content: center;
  padding: 24px;
  border-top: 1px solid #f1f5f9;
}

.custom-pagination {
  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;

      &.is-active {
        background: #667eea;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
    }
  }
}

// H5 响应式设计
@media (max-width: 768px) {
  .page-header {
    padding: 20px;
    margin-bottom: 16px;

    .title-text {
      h2 {
        font-size: 20px;
      }

      p {
        font-size: 13px;
      }
    }
  }

  .search-container {
    padding: 16px;

    .search-row {
      flex-direction: column;
      gap: 16px;

      .search-item {
        min-width: auto;
      }
    }

    .search-actions {
      flex-direction: column;

      .search-btn,
      .reset-btn,
      .add-btn {
        width: 100%;
        justify-content: center;
      }
    }
  }

  .table-container {
    overflow-x: auto;

    .voice-table {
      min-width: 800px;
    }
  }

  .action-buttons {
    flex-direction: column;
    gap: 4px;

    .action-btn {
      .action-text {
        display: none;
      }
    }
  }
}

@media (max-width: 480px) {
  .page-header {
    padding: 16px;

    .page-title {
      gap: 12px;

      .title-icon {
        font-size: 24px;
      }

      .title-text {
        h2 {
          font-size: 18px;
        }
      }
    }
  }

  .search-container {
    padding: 12px;
  }

  .voice-table {
    :deep(.el-table__header) {
      th {
        padding: 12px 8px;
        font-size: 13px;
      }
    }

    :deep(.el-table__body) {
      tr td {
        padding: 12px 8px;
      }
    }
  }

  .btn-text {
    display: none;
  }
}

// 深色模式支持
@media (prefers-color-scheme: dark) {
  .search-container,
  .table-container {
    background: #1e293b;
    border-color: #334155;
  }

  .voice-table {
    :deep(.el-table__header) {
      th {
        background: #0f172a !important;
        color: #f1f5f9;
        border-color: #334155;
      }
    }

    :deep(.el-table__body) {
      tr {
        &:hover {
          background: #334155 !important;
        }

        td {
          border-color: #334155;
        }
      }
    }
  }

  .name-cell .file-name,
  .content-cell .content-text {
    color: #f1f5f9;
  }

  .file-path-cell {
    &:hover {
      background: rgba(59, 130, 246, 0.1);
    }
  }
}
</style>
